// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'babel-polyfill'
import registComponents from './regist-components'
import Request from './request/index.js'
import * as utils from './util'
import axios from 'axios'
Vue.use(ElementUI)
registComponents(Vue)
// 挂载全局参数与方法
Vue.prototype.$utils = utils
Vue.prototype.$request = Request
Vue.config.productionTip = false
require('./assets/less/common.less')
async function getConfigJson () {
  await axios.get('static/serverconfig.json').then((result) => {
    console.log('read from json', result)
    window.webSocket = new WebSocket(result.data.webSocketURL)
    window.localStorage.setItem('serverHost', result.data.serverHost)
    window.localStorage.setItem('clientId', result.data.clientId)
  }).catch((error) => {
    window.webSocket = new WebSocket('ws://192.168.254.51:8888/websocket/websocket')
    window.localStorage.setItem('serverHost', 'https://192.168.254.98:6100')
    window.localStorage.setItem('clientId', 'C178C7C262F7185808C17D78368AF716')
    console.log(error)
  })// eslint-disable-next-line
  new Vue({
    el: '#app',
    router,
    components: {App},
    template: '<App/>'
  })
}
getConfigJson()
router.beforeEach((to, from, next) => {
  let at = window.localStorage.getItem('accessToken')
  let ak = window.localStorage.getItem('accessTokenKey')
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!at || !ak) {
      Message({
        type: 'error',
        message: '登录已过期，即将跳转到登录页面！',
        duration: 1400
      })
      setTimeout(() => {
        next({path: '/'})
      }, 1500)
    } else {
      next()
    }
  } else {
    next()
  }
})
